/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package historyBean;

import java.rmi.RemoteException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Session Bean for work with history
 * @author Konstantin
 */
public class HistoryBean implements SessionBean {
    
    private SessionContext sessionContext;
    private DataSource dataSource;
    private Connection connection;
    
    public void setSessionContext(SessionContext ctx) {
        sessionContext = ctx;
    }

    public void ejbRemove() throws EJBException, RemoteException {
        
    }

    public void ejbActivate() throws EJBException, RemoteException {
        
    }

    public void ejbPassivate() throws EJBException, RemoteException {
        
    }
    
    public void ejbCreate() throws RemoteException, CreateException{
        
    }
    
    void setConnection(){ 
        try {
            InitialContext initialContext = new InitialContext();
            dataSource = (DataSource) initialContext.lookup(
                        "java:/ATeamConnectionPool");
            System.out.println("dataSource is found");                
        } catch (NamingException e){
            e.getMessage();
        }
        if (connection == null){
            try {
                connection = dataSource.getConnection();
                System.out.println("Connection is ready");
            } catch (SQLException e){
                e.getMessage();
            }
        }    
    }
    
    void closeConnection() {
        try {
            if (connection != null){
                connection.close();
                System.out.println("Connection closed");
            }
        } catch (SQLException e) {
            e.getMessage();
        } finally {
            connection = null;
        }
    }
    
    /**
     * Loads history from database for object
     * @param objectId 
     * @return Collection message line
     */
    public Collection loadHistory(int objectId) {
        
        Collection coll = new ArrayList();
        PreparedStatement loadHistoryStat = null;
        ResultSet res = null;
        
        String loadHistory =
                "SELECT ActionId, Description, Datum"+
                " FROM History"+
                " WHERE ObjectId = ?"+
                " ORDER BY Datum";
        
        try{
            setConnection();
            loadHistoryStat = 
                connection.prepareStatement(loadHistory);
            
            loadHistoryStat.setInt(1, objectId);
            
            res = loadHistoryStat.executeQuery();
            
            while(res.next()){
                String[] result = new String[3];
                switch(res.getInt("ActionId")){
                    case 1: {
                        result[0] = "Create";
                        break;
                    }
                    case 2: {
                        result[0] = "Edit";
                        break;
                    }
                    case 3: {
                        result[0] = "CreateChild";
                        break;
                    }
                    case 4: {
                        result[0] = "DelChild";
                        break;
                    }    
                }
                result[1] = res.getString("Description");
                result[2] = res.getString("Datum");
                
                coll.add(result);
            }
        }
        catch(SQLException ex) {
            throw new EJBException(ex);
        }
        finally{
            closeConnection();
        }    
        return coll;
    }
}